package com.orkun.api.converter.impl;

import com.orkun.api.converter.core.AbsModel;
import com.orkun.api.converter.core.Sort;

/*
 * Address Info Model used as the intermediary encapsulating object during conversion
 */
public class AddressInfoModel extends AbsModel {

  public static final String ADDRESS_INFO = "AddressInfo";
  public static final String DISTRICT = "District";
  public static final String CITY = "City";
  public static final String NAME = "name";
  public static final String CODE = "code";
  public static final String ZIP = "Zip";

  public static final String DISTRICT_NAME = "DistrictName";
  public static final String CITY_NAME = "CityName";
  public static final String CITY_CODE = "CityCode";
  public static final String ZIP_CODE = "ZipCode";

  // Constructor
  public AddressInfoModel(String cityName, String cityCode, String districtName, String zipCode) {
    this.setCityName(cityName);
    this.setCityCode(cityCode);
    this.setDistrictName(districtName);
    this.setZipCode(zipCode);
  }

  public AddressInfoModel() {}

  /**
   * @return the cityName
   */
  public String getCityName() {
    return cityName;
  }

  /**
   * @param cityName the cityName to set
   */
  public void setCityName(String cityName) {
    this.cityName = cityName;
  }

  /**
   * @return the cityCode
   */
  public String getCityCode() {
    return cityCode;
  }

  /**
   * @param cityCode the cityCode to set
   */
  public void setCityCode(String cityCode) {
    this.cityCode = cityCode;
  }

  /**
   * @return the districtName
   */
  public String getDistrictName() {
    return districtName;
  }

  /**
   * @param districtName the districtName to set
   */
  public void setDistrictName(String districtName) {
    this.districtName = districtName;
  }

  /**
   * @return the zipCode
   */
  public String getZipCode() {
    return zipCode;
  }

  /**
   * @param zipCode the zipCode to set
   */
  public void setZipCode(String zipCode) {
    this.zipCode = zipCode;
  }

  @Override
  public String toString() {
    return cityName + ", " + cityCode + ", " + districtName + ", " + zipCode;
  }

  public String[] toArray() {
    return new String[] {cityName, cityCode, districtName, zipCode};
  }

  private String cityName;
  private String cityCode;
  private String districtName;
  private String zipCode;

  public boolean filterBy(String field, String value) throws Exception {
    return value.equalsIgnoreCase(getFieldValue(field));
  }

  public int compareBy(AbsModel other, Sort sort) throws Exception {
    String field = sort.getField();
    String thisVal = this.getFieldValue(field);
    String otherVal = ((AddressInfoModel) other).getFieldValue(field);
    int res = thisVal.compareTo(otherVal);
    return sort.getType() == Sort.Type.ASC ? res : res * -1;
  }

  private String getFieldValue(String field) throws Exception {

    if (field.equalsIgnoreCase(CITY_NAME) || field.equalsIgnoreCase(CITY)) {
      return getCityName();
    } else if (field.equalsIgnoreCase(CITY_CODE) || field.equalsIgnoreCase(CODE)) {
      return getCityCode();
    } else if (field.equalsIgnoreCase(DISTRICT) || field.equalsIgnoreCase(DISTRICT_NAME)) {
      return getDistrictName();
    } else if (field.equalsIgnoreCase(ZIP) || field.equalsIgnoreCase(ZIP_CODE)) {
      return getZipCode();
    } else {
      String err =
          "Error getting field value make sure you are using one of the following field names for filtering  : \n"
              + AddressInfoModel.DISTRICT_NAME
              + "\n"
              + AddressInfoModel.CITY_NAME
              + "\n"
              + AddressInfoModel.CITY_CODE + "\n" + AddressInfoModel.ZIP_CODE;

      throw new Exception(err);
    }
  }

  public static String[] getColumns() {
    return new String[] {CITY_NAME, CITY_CODE, DISTRICT_NAME, ZIP_CODE};
  }
}
